#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QTimer>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_isModified(false)
    , m_counter(0)
{
    ui->setupUi(this);

    insertAboveAction = new QAction(tr("插入上行"));
    connect(insertAboveAction, &QAction::triggered, this, &MainWindow::onInsertAboveCurrentRow);
    insertBelowAction = new QAction(tr("插入下行"));
    connect(insertBelowAction, &QAction::triggered, this, &MainWindow::onInsertBelowCurrentRow);
    deleteRowAction = new QAction(tr("删除当前行"));
    connect(deleteRowAction, &QAction::triggered, this, &MainWindow::onDeleteCurrentRow);

    ui->tableWidget->addAction(insertAboveAction);
    ui->tableWidget->addAction(insertBelowAction);
    // 插入分隔符，需要用 QAction 代替
    QAction *actSeparator = new QAction(this);
    actSeparator->setSeparator(true);
    ui->tableWidget->addAction(actSeparator);
    ui->tableWidget->addAction(deleteRowAction);
    ui->tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);

    ui->tableWidget->setColumnCount(5);
    for(int i = 0; i < 10; ++i) {
        ui->tableWidget->insertRow(i);
    }

    ui->tableWidget->setDragDropMode(QAbstractItemView::InternalMove);
    ui->tableWidget->setDragEnabled(true);
    ui->tableWidget->setAcceptDrops(true);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
//    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    saveByThread = new SaveByThread;

    saveByThread->setPath(QApplication::applicationDirPath() + "/temp.txt");

    saveTempTimer = new QTimer;
    connect(saveTempTimer, &QTimer::timeout, this, &MainWindow::onSaveTempFile);
//    saveTempTimer->start(10 * 1000);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onSaveTempFile()
{
    if(m_isModified) {
        qDebug() << "Actions modified";
    } else {
        qDebug() << "Not modified";
    }
}

void MainWindow::on_tableWidget_cellChanged(int row, int column)
{
    m_isModified = true;
    m_counter++;

//    qDebug() << QString("cellChanged(%1,%2)").arg(row).arg(column);

    QString text = QString("counter: %1, cellChanged(%2,%3)\n").arg(m_counter).arg(row).arg(column);

    int i = 0;
    int j = 0;
    for(i = 0; i < 10; ++i) {
        for(j = 0; j < 4; ++j) {
            // 注意：如果表格为空的情况下，item() 会返回 nullptr，可能引起代码错误
            if(nullptr != ui->tableWidget->item(i, j)) {
                text += ui->tableWidget->item(i, j)->data(Qt::DisplayRole).toString() + ",";
            } else {
                text += ",";
            }
        }
        if(nullptr != ui->tableWidget->item(i, j)) {
            text += ui->tableWidget->item(i, j)->data(Qt::DisplayRole).toString() + "\n";
        } else {
            text += "\n";
        }
    }

    saveByThread->setContext(text);
    saveByThread->start();
}

void MainWindow::on_pushButton_clicked()
{
    QTableWidgetItem *item = nullptr;
    for(int i = 0; i < 10; ++i) {
        item = new QTableWidgetItem(QString("%1").arg(i * 100 + 0));
        ui->tableWidget->setItem(i, 0, item);
        item = new QTableWidgetItem(QString("%1").arg(i * 100 + 1));
        ui->tableWidget->setItem(i, 1, item);
        item = new QTableWidgetItem(QString("%1").arg(i * 100 + 2));
        ui->tableWidget->setItem(i, 2, item);
        item = new QTableWidgetItem(QString("%1").arg(i * 100 + 3));
        ui->tableWidget->setItem(i, 3, item);
        item = new QTableWidgetItem(QString("%1").arg(i * 100 + 4));
        ui->tableWidget->setItem(i, 4, item);
    }
}

void MainWindow::onInsertAboveCurrentRow()
{
    int curRow = ui->tableWidget->currentRow();
    ui->tableWidget->insertRow(curRow);
    ui->tableWidget->selectRow(curRow);
}

void MainWindow::onInsertBelowCurrentRow()
{
    int curRow = ui->tableWidget->currentRow();
    ui->tableWidget->insertRow(curRow + 1);
    ui->tableWidget->selectRow(curRow + 1);
}

void MainWindow::onDeleteCurrentRow()
{
    int curRow = ui->tableWidget->currentRow();
    ui->tableWidget->removeRow(curRow);
}
